<template>
  <div v-if="row.actions.length > 2">
    <a @click="handleClickMore(row.actions[0].name, row)">{{
      row.actions ? row.actions[0].label : ""
    }}</a>
    <Divider type="vertical" />
    <Dropdown @on-click="(name) => handleClickMore(name, row)" transfer>
      <a>更多 <Icon type="ios-arrow-down" /></a>
      <DropdownMenu slot="list">
        <DropdownItem
          v-for="(item, i) in row.actions.slice(1)"
          :key="i"
          :name="item.name"
          >{{ item.label }}</DropdownItem
        >
      </DropdownMenu>
    </Dropdown>
  </div>
  <div v-else>
    <a
      v-if="row.actions && showBtn(row.actions[0].label)"
      @click="handleClickMore(row.actions[0].name, row)"
      >{{ row.actions[0].label }}</a
    >
    <Divider v-if="row.actions && row.actions.length > 1" type="vertical" />
    <a
      v-if="
        row.actions && row.actions.length > 1 && showBtn(row.actions[1].label)
      "
      @click="handleClickMore(row.actions[1].name, row)"
      >{{ row.actions[1].label }}</a
    >
  </div>
</template>
<script>
import dynamictableactions from '@/mixins/dynamictableactions'; // 混入通用代码
import { ApiRequest } from '@api/basePage';
import { mapGetters } from 'vuex';
import { getBtnsId } from '@/libs/btnsPermission';
export default {
  mixins: [dynamictableactions],
  data () {
    return {
      actions: []
    };
  },
  created () {},
  computed: {
    ...mapGetters('permissions/systemBtns', ['showBtn'])
  },
  methods: {
    //  操作更多选择
    handleClickMore (name, row) {
      if (name === 'Delete') {
        this.$Modal.confirm({
          title: '删除操作',
          content: '确定删除此数据吗？',
          onOk: () => {
            let curBtnId = getBtnsId('删除');
            sessionStorage.setItem('buttonId', curBtnId);
            let data = {
              uuids: row.uuid
            };
            // 开始请求商品列表
            ApiRequest(
              this.requestApi.delTableList.url,
              this.requestApi.delTableList.type,
              data,
              true
            ).then(res => {
              if (res.retData == false) {
                this.$Message.success('删除成功');
                this.actionsChange();
              } else {
                this.$Message.error('已被使用不可删除');
                this.actionsChange();
              }
            });
          }
        });
      } else {
        let obj = {}; // 动作的对象信息
        row.actions.forEach(item => {
          if (name === item.name) {
            obj = item;
            return false;
          }
        });
        this.setTableRowData(row);
        let curBtnId = getBtnsId('编辑');
        sessionStorage.setItem('buttonId', curBtnId);
        if (obj.handleType === 'link') {
          // 操作类型为链接时，拼接参数跳转
          let jumpPath = this.jumpPath + obj.name;
          this.$router.push({ path: jumpPath, query: { uuid: row.uuid } });
        } else {
          // 操作类型为弹窗时 打开弹窗
        }
      }
    }
  }
};
</script>
